<template>
  <div id="LabTMLBDict">
    <Breadcrumb content="批次调整" v-if="showBreadCrumbTag === true" />
    <GoBack :content="'批次调整'" v-if="showBreadCrumbTag === false" @closeDrawer="closeDrawer" :backUrl="url"></GoBack>
    <div class="bodyContent">
      <Splitpanes class="area">
        <Pane size="20">
          <el-scrollbar style="height: 100%">
            <div style="width: 95%">
              <el-form ref="formRef" :rules="formRules" :model="formData" label-width="80px" inline-message>
                <el-form-item label="工作组" prop="wtId">
                  <SelectWorkGroup style="width: 100%" ref="selectWorkGroupRef" :url="'/api/rptGroup/findRptGroup'" :inputWidth="'100%'" @getSelectedId="getSelectedId"></SelectWorkGroup>
                </el-form-item>
                <el-form-item label="标本号" prop="sampleNo">
                  <el-input v-model="formData.sampleNo" placeholder="请输入1,2,5-8等格式" clearable></el-input>
                </el-form-item>
                <el-form-item label="标本日期" prop="scheduleDate">
                  <el-date-picker style="width: 100%" :popper-style="{ zIndex: 9999999 }" :tabindex="999000" v-model="formData.scheduleDate" type="date" placeholder="选择标本日期" />
                </el-form-item>
                <el-form-item label="病人类型" prop="visitTypeIds">
                  <el-checkbox-group style="width: 100%" v-model="formData.visitTypeIds" @change="(items) => checkedChange(items, 'visitTypeIds')">
                    <el-checkbox style="width: 100%" v-for="(item, index) in visitTypeList" :label="item.code" :key="index" border>{{ item.name }}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
                <el-form-item label="调整项目" prop="labItemDictId">
                  <el-select style="width: 100%" v-model="formData.labItemDictId" collapse-tags filterable placeholder="请选择">
                    <el-option v-for="item in labItemDictList" :key="item.id" :label="item.itemNa" :value="item.itemNo"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="调整系数" prop="ratio" :item-render="{}">
                  <template #default="{}">
                    <vxe-input v-model="formData.ratio" type="number" clearable></vxe-input>
                  </template>
                </el-form-item>
                <el-form-item label="调整误差" prop="step" :item-render="{}">
                  <template #default="{}">
                    <vxe-input v-model="formData.step" type="number" :step="0.1" clearable></vxe-input>
                  </template>
                </el-form-item>
                <el-form-item label="使用公式" prop="needFormulae" :item-render="{}">
                  <template #default="{}">
                    <vxe-switch v-model="formData.needFormulae" open-label="是" close-label="否"></vxe-switch>
                  </template>
                </el-form-item>
                <el-form-item v-show="formData.needFormulae" label="调整公式" prop="formulae" :item-render="{}">
                  <template #default="{}">
                    <vxe-textarea v-model="formData.formulae" placeholder="请输入包含 R （检验结果）的表达式。如：R*1.1，为结果调整原来的1.1倍" :maxlength="2000" :autosize="{ minRows: 4, maxRows: 10 }" showWordCount></vxe-textarea>
                  </template>
                </el-form-item>
                <el-form-item label="调整原因" prop="amendedReason" :item-render="{}">
                  <template #default="{}">
                    <vxe-textarea v-model="formData.amendedReason" :maxlength="2000" :autosize="{ minRows: 4, maxRows: 10 }" showWordCount></vxe-textarea>
                  </template>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="selectWtIdChange">查询</el-button>
                  <el-button type="default" @click="resetForm">重置</el-button>
                </el-form-item>
              </el-form>调整结果 = 原始结果(R) * 调整系数 + 误差
            </div>
          </el-scrollbar>

          <!-- 中间表格+右侧表格组件  -->
        </Pane>
        <pane size="80">
          <InspectResult ref="wLResultRef" />
        </pane>
      </Splitpanes>
    </div>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import Breadcrumb from '@/components/Breadcrumb.vue'; //面包屑
import GoBack from '@/components/GoBack.vue'; //返回组件
import { Splitpanes, Pane } from 'splitpanes';
import { reactive, ref, getCurrentInstance, onMounted, watch } from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common.ts';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import SelectWorkGroup from '../lab_bus/components/SelectWorkGroup.vue'; //工作组引入下拉组件
import InspectResult from './InspectResult.vue'; //中间表格+右侧表格组件
import { GetRptGroupOptions } from '@/utils/SysOptionsHelper';

// 全局变量定义区 ==========
defineExpose({
  init
});

onMounted(() => {
  getDictPatTypeList();
  getSampleType();
});

let showBreadCrumbTag = ref(true); //当为true时显示的是普通面包屑 false显示的是返回组件
let url = ref(''); //抽屉要返回的路由地址

function init(row: any, row2: any) {
  url.value = row;
  showBreadCrumbTag.value = !row2;
}

// 表单校验规则
const formRules = reactive<VxeFormPropTypes.Rules>({
  labItemDictId: [{ required: true, message: '请选择调整项目' }]
});

// 关闭抽屉
function closeDrawer() {
  showBreadCrumbTag.value = true; //恢复成面包屑
  proxy.$emit('closeDrawer');
}

let { proxy }: any = getCurrentInstance();
let selectWorkGroupRef = ref(null);
const formRef = ref(); //表单ref标记
//病人类型
const visitTypeList: any[] = reactive([]);
//样本类型
const specTypeList = reactive([]);

const labItemDictList = reactive([]);

// 表单绑定的数据类型限制
interface FormDataVO {
  sampleNo: string /*标本号*/;
  isFilterNoPatName: boolean /*无病人姓名样本*/;
  isFilterNoResult: boolean /*无检验结果样本*/;
  isFilterNoSpecCode: boolean /*无条码号样本*/;
  isPrinted: Number /*审核标志*/;
  isReviewed: Number /*打印标志*/;
  skipCount: Number /*样本类型最小数*/;
  maxResultCount: Number /*样本类型最大数*/;
  scheduleDate: string /*标本日期开始时间*/;
  labItemDictId: string;
  ratio: Number;
  step: Number;
  needFormulae: number;
  formulae: string;
  amendedReason: string;
}
// 表单绑定的数据
let formData = reactive<FormDataVO>({
  sampleNo: '',
  isFilterNoPatName: false,
  isFilterNoResult: false,
  isFilterNoSpecCode: false,
  isPrinted: null,
  isReviewed: null,
  maxResultCount: 0,
  scheduleDate: formatDateTime(new Date(), 'yyyy-MM-dd'),
  skipCount: 0,
  labItemDictId: '',
  ratio: 0,
  step: 0,
  needFormulae: 0,
  formulae: '',
  amendedReason: ''
});
let wLResultRef = ref(null); //引用中间表格+右侧表格组件ref

// 方法定义区 =============
onMounted(() => {
  getSelectedId; // 接收子组件传过来的选中的id(选择的那个工作组的id)
  //selectWtIdChange(); // 左侧表单查询按钮
  //getLabItemDictList();
});

// 接收子组件传过来的选中的id(选择的那个工作组的id)
function getSelectedId(propValue: any) {
  formData.labItemDictId = '';
  GetRptGroupOptions(propValue.value);
  formData.wtId = propValue.value;
  getLabItemDictList();
}
// 获取报告项目列表
function getLabItemDictList() {
  labItemDictList.length = 0;
  const url = '/lis/lab/item/findLabItemDictList';
  proxy.$http
    .get(url, { params: { rptGroup: formData.wtId } })
    .then((res: any) => {
      Object.assign(labItemDictList, res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}

// 多选框绑定值变化
function checkedChange(items, name, isChange) {
  formData[name] = items;
  processItemChange(isChange);
}

// 筛选条件过滤
function processItemChange(isChange) {
  if (!isChange) {
    return;
  }
  if (formData.processItem.length > 0) {
    formData.processItem.forEach((item) => {
      formData[item] = true;
    });
  } else {
    formData.isFilterNoPatName = false;
    formData.isFilterNoResult = false;
    formData.isFilterNoSpecCode = false;
  }
  //selectWtIdChange();
}

// 重置表单
function resetForm() {
  formData.sampleNo = '';
  formData.isFilterNoPatName = false;
  formData.isFilterNoResult = false;
  formData.isFilterNoSpecCode = false;
  formData.isPrinted = null;
  formData.isReviewed = null;
  formData.maxResultCount = 0;
  formData.scheduleDate = '';
  formData.labItemDictId = '';
  formData.ratio = 0;
  formData.step = 0;
  formData.skipCount = 0;
  formData.needFormulae = 0;
  formData.formulae = '';
  formData.amendedReason = '';

  selectWorkGroupRef.value?.resetSelectedVal(); //重置选中的工作组
}

// 查询按钮
function selectWtIdChange() {
  const el: any = proxy.$refs['formRef'];
  formRef.value?.validate((valid: any) => {
    if (!valid) {
      VxeMessage('校验表单失败', VxeNotifyType.error);
      return false;
    }
    wLResultRef.value?.getWklt(formData);
  });
}
//获取病人类型数据字典
function getDictPatTypeList() {
  visitTypeList.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'PATTYPE' } })
    .then((res: any) => {
      //patTypeList.push(...res.data);
      Object.assign(visitTypeList, res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
//获取样本类型
function getSampleType() {
  specTypeList.length = 0;
  const url = '/api/lab/labSampleType/findSampleTypeList';
  proxy.$http
    .get(url)
    .then((res: any) => {
      //patTypeList.push(...res.data);
      Object.assign(specTypeList, res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
</script>
<style lang="scss" scoped>
#LabTMLBDict {
  width: 100%;
  height: 100%;

  .bodyContent {
    // 面包屑40px  上下间距10px
    height: calc(100% - 60px);

    .area {
      background-color: $base-white;
      height: 100%;

      :deep(.el-scrollbar__view) {
        height: 100%;

        .el-form {
          height: 100%;
          padding-top: 15px;
          box-sizing: border-box;

          .el-checkbox {
            margin: 8px 10px 0 0;
          }
        }
      }
    }
  }
}
</style>
